﻿using System;
using System.Windows.Forms;
using SSISComponents.ReportGenerator.ReportService2005;

namespace SSISComponents.ReportGenerator
{
    public partial class ReportServerUI : Form
    {

        private string _Reportname = null;
        private string _ReportServer = null;
        private string _Username = null;
        private string _Password = null;
        private bool _WindowsAuthorization = false;
        ReportService2005.ReportingService2005 rs;

        /// <summary>
        /// 
        /// </summary>
        public string Reportname
        {
            get { return _Reportname; }
            set { _Reportname = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public string ReportServer
        {
            get { return _ReportServer; }
            set { _ReportServer = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public string Username
        {
            get { return _Username; }
            set { _Username = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public string Password
        {
            get { return _Password; }
            set { _Password = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public bool WindowsAuthorization
        {
            get { return _WindowsAuthorization; }
            set { _WindowsAuthorization = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public ReportServerUI()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (tvReportServer.SelectedNode != null)
            {
                CatalogItem Item = (CatalogItem)tvReportServer.SelectedNode.Tag;
                if (Item.Type == ItemTypeEnum.Report)
                {
                    _Reportname = Item.Path.ToString();
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("Please select a Report.");
                }
            }
            else
            {
                MessageBox.Show("Please select a Report.");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ReportServer_Load(object sender, EventArgs e)
        {
            rs = new ReportService2005.ReportingService2005();
            rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
            rs.Url = ReportServer + "/ReportService2005.asmx";

            tvReportServer.BeginUpdate();
            tvReportServer.Nodes.Clear();

            tvReportServer.Nodes.Add(GetFolderAsNodes());
            tvReportServer.ExpandAll();

            tvReportServer.EndUpdate();
            
        }

        /// <summary>
        /// build root element and load folders and reports
        /// </summary>
        /// <returns></returns>
        private TreeNode GetFolderAsNodes()
        {
            TreeNode XRoot = new TreeNode(ReportServer);
            XRoot.Tag = rs.Url;
            XRoot.ImageIndex = 2;
            return (FillTreeView("/", XRoot));
        }

        /// <summary>
        /// get all folders and reports as nodes
        /// </summary>
        /// <param name="Path"></param>
        /// <param name="ParentNode"></param>
        /// <returns></returns>
        private TreeNode FillTreeView(string Path, TreeNode ParentNode)
        {
            CatalogItem[] CItems;
            CItems = rs.ListChildren(Path, false);
            foreach (CatalogItem Item in CItems)
            {
                switch (Item.Type)
                {
                    case ItemTypeEnum.Folder:
                        TreeNode SubNode = new TreeNode(Item.Name);
                        SubNode.ImageIndex = 1;
                        SubNode.Tag = Item;
                        SubNode.Name = Item.Path;
                        SubNode.SelectedImageIndex = SubNode.ImageIndex;
                        ParentNode.Nodes.Add(FillTreeView(Item.Path, SubNode));
                        break;
                    case ItemTypeEnum.Report:
                        TreeNode ReportNode = new TreeNode(Item.Name);
                        ReportNode.ImageIndex = 0;
                        ReportNode.Tag = Item;
                        ReportNode.Name = Item.Path;
                        ReportNode.SelectedImageIndex = ReportNode.ImageIndex;
                        ParentNode.Nodes.Add(ReportNode);
                        break;
                }
            }
            return ParentNode;
        }

        /// <summary>
        /// Set the selected report as current report
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tvReportServer_AfterSelect(object sender, EventArgs e)
        {
            CatalogItem Item = (CatalogItem)tvReportServer.SelectedNode.Tag;
            if (Item.Type == ItemTypeEnum.Report)
            {
                _Reportname = Item.Path.ToString();
                this.DialogResult = DialogResult.OK;
            }
        }

        private void previewReportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CatalogItem Item = (CatalogItem)tvReportServer.SelectedNode.Tag;
            if (Item.Type == ItemTypeEnum.Report)
            {
                SSISComponents.ReportGenerator.ReportViewerUI localReport = new SSISComponents.ReportGenerator.ReportViewerUI();
                localReport.Reportserver = _ReportServer;
                localReport.ReportType = "SQL Server Report";
                localReport.Reportname = Item.Path.ToString();
                localReport.WindowsAuthorization = _WindowsAuthorization;
                localReport.Username = _Username;
                localReport.Password = _Password;
                localReport.ShowDialog();
            }
        }

        private void propertiesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                CatalogItem Item = (CatalogItem)tvReportServer.SelectedNode.Tag;
                if (Item.Type == ItemTypeEnum.Report)
                {
                    ReportProperties RepProperties = new ReportProperties();
                    RepProperties.Item = Item;
                    RepProperties.ReportServer = _ReportServer;
                    RepProperties.Reportname = _Reportname;
                    RepProperties.Username = _Username;
                    RepProperties.Password = _Password;
                    RepProperties.ShowDialog();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("An unknown error has occurred.", "SSIS ReportGeneratorTask");
            }
        }
        
    }
}
